#include "AddBookmarkDialog.h"
#include "AddBookmarkProxyModel.h"
#include "BookmarksManager.h"
#include "BookmarksModel.h"
#include "BookmarkNode.h"

#include <QTreeView>
#include <QSettings>
#include <QMessageBox>


AddBookmarkDialog::AddBookmarkDialog(const QString &url, const QString &title, const QString& default_folder, BookmarksManager *bookmarkManager, QWidget *parent)
    : QDialog(parent)
    , m_url(url)
    , m_default_folder(default_folder)
    , m_bookmarksManager(bookmarkManager)
{
    setWindowFlags(Qt::Sheet);

    setupUi(this);
    QTreeView *view = new QTreeView(this);
    m_proxyModel = new AddBookmarkProxyModel(this);
    BookmarksModel *model = m_bookmarksManager->bookmarksModel();
    m_proxyModel->setSourceModel(model);
    view->setModel(m_proxyModel);
    view->expandAll();
    view->header()->setStretchLastSection(true);
    view->header()->hide();
    view->setItemsExpandable(false);
    view->setRootIsDecorated(false);
    view->setIndentation(10);
    location->setModel(m_proxyModel);
    view->show();
    location->setView(view);
    BookmarkNode *menu = m_bookmarksManager->toolbar();
    QModelIndex idx = m_proxyModel->mapFromSource(model->index(menu));
    QSettings settings;
    settings.beginGroup(QLatin1String("websettings"));
    if (!m_default_folder.isEmpty())
    {
        BookmarkNode* folder = m_bookmarksManager->find_folder(NULL, m_default_folder);
        if (folder)
        {
            QModelIndex ix = m_proxyModel->mapFromSource(model->index(folder));
            if (ix.isValid())
                idx = ix;
        }
        else
        {
            settings.setValue(QLatin1String("addBookmarkDefaultLocation"), "");
            settings.setValue(QLatin1String("addBookmarkUseDefaultLocation"), false);
        }
    }
    view->setCurrentIndex(idx);
    location->setCurrentIndex(idx.row());
    name->setText(title);
    chkSetDefault->setChecked(settings.value(QLatin1String("addBookmarkUseDefaultLocation"), false).toBool());
}

void AddBookmarkDialog::accept()
{
    QModelIndex index = location->view()->currentIndex();
    index = m_proxyModel->mapToSource(index);
    if (!index.isValid())
        index = m_bookmarksManager->bookmarksModel()->index(0, 0);
    BookmarkNode *parent = m_bookmarksManager->bookmarksModel()->node(index);

    for (int i = parent->children().count() - 1; i >= 0; --i)
    {
        BookmarkNode *node = parent->children().at(i);
        if (node && (node->title == name->text() || node->url == m_url) )
        {
            QMessageBox::StandardButton button = QMessageBox::information(this, tr("Duplicate bookmark"),
                tr("Bookmark having the same title or url already exists in your folder.<br>Are you sure to add another one?"),
                QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);

            if (button == QMessageBox::Cancel)
                return;
        }
    }

    BookmarkNode *bookmark = new BookmarkNode(BookmarkNode::Bookmark);
    bookmark->url = m_url;
    bookmark->title = name->text();

    m_bookmarksManager->addBookmark(parent, bookmark);

    QSettings settings;
    settings.beginGroup(QLatin1String("websettings"));
    settings.setValue(QLatin1String("addBookmarkUseDefaultLocation"), chkSetDefault->isChecked());
    settings.setValue(QLatin1String("addBookmarkDefaultLocation"), parent->title);

    QDialog::accept();
}

void AddBookmarkDialog::acceptDefaultLocation()
{
    accept();
}
